<?php

namespace App\Kuafu\Banner\Controller\B;

use App\Kuafu\Banner\Enum\BannerCodeEnum;
use App\Kuafu\Banner\Exceptions\BannerException;
use App\Kuafu\Banner\Service\BannerService;
use App\Kuafu\Banner\Validate\BannerValidate;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class BannerController extends BaseController
{

    /**
     * 横幅添加
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws BannerException
     */
    public function addBanner(Request $request)
    {
        $valid = BannerValidate::addOrSetBanner($request);

        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, $valid);
        }
        BannerService::addBanner($request);
        return $this->success();
    }

    /**
     * 横幅列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBanner()
    {
        $bannerList = BannerService::getBanner();
        return $this->success($bannerList);
    }

    /**
     * 横幅删除
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws BannerException
     * @throws \App\Kuafu\User\Exceptions\UserException
     */
    public function delBanner(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = BannerValidate::delAdList(['id' => $id]);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, '参数校验失败');
        }

        BannerService::delBanner($id);
        return $this->success();
    }

    /**
     * 横幅修改
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws BannerException
     */
    public function setBanner(Request $request)
    {
        $valid = BannerValidate::addOrSetBanner($request);

        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, $valid);
        }
        BannerService::setBanner($request);
        return $this->success();
    }

    /**
     * 横幅详情
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBannerById(Request $request, $id)
    {
        $id = (int)$id;

        $valid = BannerValidate::getBannerById($request);
        if ($valid) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, '非法数据，参数校验失败');
        }
        $info = BannerService::getBannerById($id);

        return $this->success($info);
    }

}
